package com.ruoyi.report_lqhhl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_lqhhl.domain.ReportLqhhlMxebgfjlbBsDetail1;
import com.ruoyi.report_lqhhl.domain.ReportLqhhlMxebgfjlbBsDetail2;
import com.ruoyi.report_lqhhl.domain.vo.ReportLqhhlMxebgfjlbBsDetail1Vo;
import com.ruoyi.report_lqhhl.domain.vo.ReportLqhhlMxebgfjlbBsDetail2Vo;
import com.ruoyi.report_lqhhl.mapper.ReportLqhhlMxebgfjlbBsDetail1Mapper;
import com.ruoyi.report_lqhhl.mapper.ReportLqhhlMxebgfjlbBsDetail2Mapper;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_lqhhl.domain.bo.ReportLqhhlMxebgfjlbBsInfoBo;
import com.ruoyi.report_lqhhl.domain.vo.ReportLqhhlMxebgfjlbBsInfoVo;
import com.ruoyi.report_lqhhl.domain.ReportLqhhlMxebgfjlbBsInfo;
import com.ruoyi.report_lqhhl.mapper.ReportLqhhlMxebgfjlbBsInfoMapper;
import com.ruoyi.report_lqhhl.service.IReportLqhhlMxebgfjlbBsInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 沥青混合料马歇尔试验检测记录(干法)Service业务层处理
 *
 * @author llb
 * @date 2024-08-19
 */
@RequiredArgsConstructor
@Service
public class ReportLqhhlMxebgfjlbBsInfoServiceImpl implements IReportLqhhlMxebgfjlbBsInfoService {

    private final ReportLqhhlMxebgfjlbBsInfoMapper baseMapper;
    private final ReportLqhhlMxebgfjlbBsDetail1Mapper detail1Mapper;
    private final ReportLqhhlMxebgfjlbBsDetail2Mapper detail2Mapper;

    /**
     * 查询沥青混合料马歇尔试验检测记录(干法)
     */
    @Override
    @FIllBasicInfo
    public ReportLqhhlMxebgfjlbBsInfoVo queryById(Long excelId){
        ReportLqhhlMxebgfjlbBsInfoVo reportLqhhlMxebgfjlbBsInfoVo = baseMapper.selectVoById(excelId);
        List<ReportLqhhlMxebgfjlbBsDetail1Vo> detail1List = detail1Mapper.selectVoList(
            new LambdaQueryWrapper<ReportLqhhlMxebgfjlbBsDetail1>().eq(ReportLqhhlMxebgfjlbBsDetail1::getExcelId, excelId));
        if (CollUtil.isNotEmpty(detail1List)) {
            reportLqhhlMxebgfjlbBsInfoVo.setDetail1List(detail1List);
        }
        List<ReportLqhhlMxebgfjlbBsDetail2Vo> detail2List = detail2Mapper.selectVoList(
            new LambdaQueryWrapper<ReportLqhhlMxebgfjlbBsDetail2>().eq(ReportLqhhlMxebgfjlbBsDetail2::getExcelId, excelId));
        if (CollUtil.isNotEmpty(detail2List)) {
            reportLqhhlMxebgfjlbBsInfoVo.setDetail2List(detail2List);
        }
        return reportLqhhlMxebgfjlbBsInfoVo;
    }

    /**
     * 查询沥青混合料马歇尔试验检测记录(干法)列表
     */
    @Override
    public TableDataInfo<ReportLqhhlMxebgfjlbBsInfoVo> queryPageList(ReportLqhhlMxebgfjlbBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportLqhhlMxebgfjlbBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportLqhhlMxebgfjlbBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询沥青混合料马歇尔试验检测记录(干法)列表
     */
    @Override
    public List<ReportLqhhlMxebgfjlbBsInfoVo> queryList(ReportLqhhlMxebgfjlbBsInfoBo bo) {
        LambdaQueryWrapper<ReportLqhhlMxebgfjlbBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportLqhhlMxebgfjlbBsInfo> buildQueryWrapper(ReportLqhhlMxebgfjlbBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportLqhhlMxebgfjlbBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, ReportLqhhlMxebgfjlbBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, ReportLqhhlMxebgfjlbBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportLqhhlMxebgfjlbBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), ReportLqhhlMxebgfjlbBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), ReportLqhhlMxebgfjlbBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportLqhhlMxebgfjlbBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ReportLqhhlMxebgfjlbBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportLqhhlMxebgfjlbBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), ReportLqhhlMxebgfjlbBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), ReportLqhhlMxebgfjlbBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), ReportLqhhlMxebgfjlbBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), ReportLqhhlMxebgfjlbBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), ReportLqhhlMxebgfjlbBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), ReportLqhhlMxebgfjlbBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ReportLqhhlMxebgfjlbBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportLqhhlMxebgfjlbBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportLqhhlMxebgfjlbBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(bo.getLiqyl() != null, ReportLqhhlMxebgfjlbBsInfo::getLiqyl, bo.getLiqyl());
        lqw.eq(StringUtils.isNotBlank(bo.getJiswd()), ReportLqhhlMxebgfjlbBsInfo::getJiswd, bo.getJiswd());
        lqw.eq(StringUtils.isNotBlank(bo.getHunhllx()), ReportLqhhlMxebgfjlbBsInfo::getHunhllx, bo.getHunhllx());
        lqw.eq(StringUtils.isNotBlank(bo.getHuanjwd()), ReportLqhhlMxebgfjlbBsInfo::getHuanjwd, bo.getHuanjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getBiaozsjcc()), ReportLqhhlMxebgfjlbBsInfo::getBiaozsjcc, bo.getBiaozsjcc());
        lqw.eq(StringUtils.isNotBlank(bo.getJiscs()), ReportLqhhlMxebgfjlbBsInfo::getJiscs, bo.getJiscs());
        lqw.eq(bo.getLilzdxdmd() != null, ReportLqhhlMxebgfjlbBsInfo::getLilzdxdmd, bo.getLilzdxdmd());
        lqw.eq(bo.getKuanglhcmtjxdmd() != null, ReportLqhhlMxebgfjlbBsInfo::getKuanglhcmtjxdmd, bo.getKuanglhcmtjxdmd());
        lqw.eq(bo.getLiqdxdmd() != null, ReportLqhhlMxebgfjlbBsInfo::getLiqdxdmd, bo.getLiqdxdmd());
        lqw.eq(bo.getTonggl() != null, ReportLqhhlMxebgfjlbBsInfo::getTonggl, bo.getTonggl());
        lqw.eq(bo.getLiqjhlbl() != null, ReportLqhhlMxebgfjlbBsInfo::getLiqjhlbl, bo.getLiqjhlbl());
        lqw.eq(bo.getYouxlqyl() != null, ReportLqhhlMxebgfjlbBsInfo::getYouxlqyl, bo.getYouxlqyl());
        lqw.eq(bo.getFenjb() != null, ReportLqhhlMxebgfjlbBsInfo::getFenjb, bo.getFenjb());
        lqw.eq(StringUtils.isNotBlank(bo.getXianwmc()), ReportLqhhlMxebgfjlbBsInfo::getXianwmc, bo.getXianwmc());
        lqw.eq(bo.getXianwyl() != null, ReportLqhhlMxebgfjlbBsInfo::getXianwyl, bo.getXianwyl());
        lqw.eq(bo.getXianwxdmd() != null, ReportLqhhlMxebgfjlbBsInfo::getXianwxdmd, bo.getXianwxdmd());
        lqw.eq(bo.getMaotjxdmdpjz() != null, ReportLqhhlMxebgfjlbBsInfo::getMaotjxdmdpjz, bo.getMaotjxdmdpjz());
        lqw.eq(bo.getKongxlpjz() != null, ReportLqhhlMxebgfjlbBsInfo::getKongxlpjz, bo.getKongxlpjz());
        lqw.eq(bo.getYouxlqtjbflpjz() != null, ReportLqhhlMxebgfjlbBsInfo::getYouxlqtjbflpjz, bo.getYouxlqtjbflpjz());
        lqw.eq(bo.getJianxlpjz() != null, ReportLqhhlMxebgfjlbBsInfo::getJianxlpjz, bo.getJianxlpjz());
        lqw.eq(bo.getLqbhdpjz() != null, ReportLqhhlMxebgfjlbBsInfo::getLqbhdpjz, bo.getLqbhdpjz());
        lqw.eq(bo.getCujlgjjxlpjz() != null, ReportLqhhlMxebgfjlbBsInfo::getCujlgjjxlpjz, bo.getCujlgjjxlpjz());
        lqw.eq(bo.getWenddpjz() != null, ReportLqhhlMxebgfjlbBsInfo::getWenddpjz, bo.getWenddpjz());
        lqw.eq(bo.getLiuzpjz() != null, ReportLqhhlMxebgfjlbBsInfo::getLiuzpjz, bo.getLiuzpjz());
        lqw.eq(bo.getMaxemspjz() != null, ReportLqhhlMxebgfjlbBsInfo::getMaxemspjz, bo.getMaxemspjz());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportLqhhlMxebgfjlbBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getJianc()), ReportLqhhlMxebgfjlbBsInfo::getJianc, bo.getJianc());
        lqw.eq(StringUtils.isNotBlank(bo.getJil()), ReportLqhhlMxebgfjlbBsInfo::getJil, bo.getJil());
        lqw.eq(StringUtils.isNotBlank(bo.getFuh()), ReportLqhhlMxebgfjlbBsInfo::getFuh, bo.getFuh());
        lqw.eq(StringUtils.isNotBlank(bo.getRiq()), ReportLqhhlMxebgfjlbBsInfo::getRiq, bo.getRiq());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), ReportLqhhlMxebgfjlbBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), ReportLqhhlMxebgfjlbBsInfo::getPandyjIds, bo.getPandyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), ReportLqhhlMxebgfjlbBsInfo::getJiancyjIds, bo.getJiancyjIds());
        return lqw;
    }

    /**
     * 新增沥青混合料马歇尔试验检测记录(干法)
     */
    @Override
    @DSTransactional
    public Boolean insertByBo(ReportLqhhlMxebgfjlbBsInfoBo bo) {
        ReportLqhhlMxebgfjlbBsInfo add = BeanUtil.toBean(bo, ReportLqhhlMxebgfjlbBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            ArrayList<ReportLqhhlMxebgfjlbBsDetail1> details1 = new ArrayList<>(8);
            for (int i=1;i<=8;i++ ){
                details1.add(new ReportLqhhlMxebgfjlbBsDetail1(bo.getExcelId()));
            }
            detail1Mapper.insertBatch(details1);
            ArrayList<ReportLqhhlMxebgfjlbBsDetail2> details2 = new ArrayList<>(6);
            for (int i=1;i<=6;i++ ){
                details2.add(new ReportLqhhlMxebgfjlbBsDetail2(bo.getExcelId()));
            }
            detail2Mapper.insertBatch(details2);
        }
        return flag;
    }

    /**
     * 修改沥青混合料马歇尔试验检测记录(干法)
     */
    @Override
    @DSTransactional
    public Boolean updateByBo(ReportLqhhlMxebgfjlbBsInfoBo bo) {
        ReportLqhhlMxebgfjlbBsInfo update = BeanUtil.toBean(bo, ReportLqhhlMxebgfjlbBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            detail1Mapper.updateBatchById(BeanUtil.copyToList(bo.getDetail1List(), ReportLqhhlMxebgfjlbBsDetail1.class));
            detail2Mapper.updateBatchById(BeanUtil.copyToList(bo.getDetail2List(), ReportLqhhlMxebgfjlbBsDetail2.class));
        }
        return flag;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportLqhhlMxebgfjlbBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除沥青混合料马歇尔试验检测记录(干法)
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
